﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TienTeTuong
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        public List<CateDetailProductDTO> lstProDetail = new List<CateDetailProductDTO>();
        public CategoryDetailDTO cateDetail = new CategoryDetailDTO();
        public List<CategoryDetailDTO> lstCateDetail = new List<CategoryDetailDTO>();
        public List<OptionDTO> lstOption = new List<OptionDTO>();
        ProductDetailBUS proBUS = new ProductDetailBUS();
        CategoryDetailBUS cateDetailBUS = new CategoryDetailBUS();
        CategoryBUS cateBUS = new CategoryBUS();
        public string CateDetailID;
        public string cateID;
        public string action;
        public int maxPerline;
        public string cateName = "";
        public int iTotalPage;
        public int iCurPage;
        public int iUpperPage;
        public int iLowerPage;
        public int iTotalResult;
        public double Price;
        public int orderCounter = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            CateDetailID = Request.QueryString["CateDetailID"] ?? "";
            cateID = Request.QueryString["CateID"] ?? "";
            action = Request.QueryString["action"] ?? "";
            //string strPage = Request.QueryString["page"];
            // page = Common.IsNumber(strPage) ? int.Parse(strPage) : 1;

            // Get list option of category detail
            //  lstOption = proBUS.GetCategoryDetailOption(CateDetailID);
            //Get info of category detail
            if (action.Equals("Edit"))
            {
                OrderDTO order = (OrderDTO)Session["Order"];
                if (order == null)
                {
                    Response.Redirect("Categories.aspx");
                }
                else
                {
                    foreach (var i in order.Items)
                    {
                        if (i.CategoryDetailID.Equals(CateDetailID))
                        {
                            lstProDetail = i.Products;
                            cateID = i.CateID;
                            break;
                        }
                    }
                    foreach (var v in lstProDetail)
                    {
                        Price += double.Parse(v.Price)*v.Quantity; // Update price by jquery, check againn when summit
                    }
                }
                cateDetail = cateDetailBUS.GetCategoryDetailByID(CateDetailID);
                lstCateDetail = cateDetailBUS.GetListCategoryDetailByCateID(cateID);
                cateName = cateBUS.GetCateNameByID(cateID);
            }
            else
            {
                cateDetail = cateDetailBUS.GetCategoryDetailByID(CateDetailID);
                // get category name
                cateName = cateBUS.GetCateNameByID(cateID);
                // get list products detail of category detail
                //if (lstOption.Count > 0)
                //{
                lstProDetail = proBUS.GetListProductDetail(CateDetailID);
                foreach (var v in lstProDetail)
                {
                    Price += double.Parse(v.Price)*v.Quantity; // Update price by jquery, check againn when summit
                }

                //}
                // get list category detail with the same category

                lstCateDetail = cateDetailBUS.GetListCategoryDetailByCateID(cateID);
                /// code phan trang tam thoi chua lam vi bi van de layout
                //lstCateDetail = cateDetailBUS.GetListCategoryByPage(cateID, page, 3, out iTotalPage,
                //                                                    out iCurPage, out iLowerPage, out iUpperPage, out iTotalResult);
            }
        }

        protected void btnCart_Click(object sender, EventArgs e)
        {
            OrderDTO curOrder = (OrderDTO)Session["Order"];
            ProductInOrder productInOrder = new ProductInOrder();
            productInOrder.CategoryDetailID = CateDetailID;

            // check if item has exist in order already
            if (curOrder == null)
            {
                OrderDTO order = new OrderDTO();
                curOrder = order;
                productInOrder.NameToDisplay = cateName + "/" + cateDetail.Name;
                if (arrProductID.Value != "")
                {
                    List<String> arrAddPD = arrProductID.Value.Split('/').ToList();
                    List<String> arrAddPDCount = arrProductCount.Value.Split('/').ToList();
                    for (int i =  0; i < arrAddPD.Count; i++)
                    {
                        if (arrAddPD[i] != "")
                        {
                            var proDetail = proBUS.getProductDetailByID(arrAddPD[i]);
                            if (proDetail != null)
                            {
                                double tmpPrice;
                                Double.TryParse(proDetail.Price, out tmpPrice);
                                proDetail.Quantity = double.Parse(arrAddPDCount[i]);
                                productInOrder.Products.Add(proDetail);
                                productInOrder.Price += tmpPrice*proDetail.Quantity;
                            }
                        }
                    }
                }
                productInOrder.CategoryQuantity = 1;
                productInOrder.CateID = cateID;
                curOrder.Items.Add(productInOrder);
            }
            else
            {
                productInOrder.NameToDisplay = cateName + "/" + cateDetail.Name;
                if (arrProductID.Value != "")
                {
                    List<String> arrAddPD = arrProductID.Value.Split('/').ToList();
                    List<String> arrAddPDCount = arrProductCount.Value.Split('/').ToList();
                    for (int i = 0; i < arrAddPD.Count; i++)
                    {
                        if (arrAddPD[i] != "")
                        {
                            var proDetail = proBUS.getProductDetailByID(arrAddPD[i]);
                            if (proDetail != null)
                            {
                                double tmpPrice;
                                Double.TryParse(proDetail.Price, out tmpPrice);
                                proDetail.Quantity = double.Parse(arrAddPDCount[i]);
                                productInOrder.Products.Add(proDetail);
                                productInOrder.Price += tmpPrice * proDetail.Quantity;
                            }
                        }
                    }
                }
                if (action.Equals("Edit"))
                {
                    for (int i = 0; i < curOrder.Items.Count; i++)
                    {
                        if (curOrder.Items[i].CategoryDetailID.Equals(CateDetailID))
                        {
                            productInOrder.CategoryQuantity = curOrder.Items[i].CategoryQuantity;
                            productInOrder.CateID = cateID;
                            curOrder.Items[i] = productInOrder;
                        }
                    }
                }
                //else if (isInOrder(curOrder, productInOrder))
                //{
                //    foreach (var item in curOrder.Items)
                //    {
                //        if (item.CategoryDetailID == CateDetailID)
                //        {
                //            foreach (var pro in productInOrder.Products)
                //            {
                //                if ( isInProducts(item, pro))
                //                {
                                    
                //                } 
                //            }
                //            //item.CategoryQuantity++;
                //        }
                //    }
                //}
                else
                {
                    productInOrder.CategoryQuantity = 1;
                    productInOrder.CateID = cateID;
                    curOrder.Items.Add(productInOrder);
                }
            }
            Session["Order"] = curOrder;
            ItemInOrderCount.Value = curOrder.Items.Count.ToString();

            if (action.Equals("Edit"))
            {
                Response.Redirect("OrderInformation.aspx");
            }
            else {
                Response.Redirect("Categories.aspx");
            }
        }
        private bool isInOrder(OrderDTO order, ProductInOrder item)
        {
            foreach (var a in order.Items)
            {
                if (a.CategoryDetailID.Equals(item.CategoryDetailID))
                {
                    //for (var i = 0; i < a.Products.Count; i++)
                    //{
                    //    if (!a.Products[i].ProId.Equals(item.Products[i].ProId))
                    //    {
                    //        return false;
                    //    }
                    //}
                    return true;
                }
            }
            return false;
        }

        private bool isInProducts(ProductInOrder productInOrder, CateDetailProductDTO item)
        {
            foreach (var a in productInOrder.Products)
            {
                if (a.ProId.Equals(item.ProId))
                {
                    //for (var i = 0; i < a.Products.Count; i++)
                    //{
                    //    if (!a.Products[i].ProId.Equals(item.Products[i].ProId))
                    //    {
                    //        return false;
                    //    }
                    //}
                    return true;
                }
            }
            return false;
        }
    }
}